package com.benmu.framework.adapter;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.android.king.fileselector.FileSelectorActivity;
import com.android.king.vo.MyFileItem;
import com.android.king.vo.MyFilePicker;
import com.benmu.framework.constant.Constant;
import com.benmu.framework.http.Api;
import com.benmu.framework.manager.ManagerFactory;
import com.benmu.framework.manager.impl.AxiosManager;
import com.benmu.framework.manager.impl.ModalManager;
import com.benmu.framework.manager.impl.ParseManager;
import com.benmu.framework.manager.impl.PermissionManager;
import com.benmu.framework.manager.impl.PersistentManager;
import com.benmu.framework.model.UploadFileBean;
import com.benmu.framework.utils.AppUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *  xum add file
 */
public class DefaultFileAdapter {
    private static DefaultFileAdapter mInstance = new DefaultFileAdapter();
    private static MyFilePicker myFilePicker = MyFilePicker.getInstance();

    private DefaultFileAdapter() {
    }

    public static DefaultFileAdapter getInstance() {
        return mInstance;
    }
    // public static int REQUESTCODE_FROM_ACTIVITY = 1000;
    // public static int REQUESTCODE_FROM_FRAGMENT = 1001;
    public void pickFile(final Context context, UploadFileBean bean, int requestCode) {
        if (!checkPermission(context)) return;
        Intent intent = new Intent(context, FileSelectorActivity.class);
        intent.putExtra(FileSelectorActivity.ACTIVITY_KEY_MULTI, true);  //是否多选模式
        intent.putExtra(FileSelectorActivity.ACTIVITY_KEY_MAX_COUNT, 5);//限定文件选择数，默认为3
        intent.putExtra(FileSelectorActivity.ACTIVITY_KEY_FILEROOT, ""); //初始路径
        intent.putExtra(FileSelectorActivity.ACTIVITY_KEY_FILE_TYPE, ""); //筛选文件类型
        PersistentManager persistentManager = ManagerFactory.getManagerService(PersistentManager.class);
        persistentManager.setCacheData(Constant.FileConstants.UPLOAD_FILE_BEAN, bean);
        ((Activity) context).startActivityForResult(intent, requestCode);
    }

    //
//    public void pickPhoto(final Context context, UploadImageBean bean, int requestCode) {
//        if (!checkPermission(context)) return;
//
//        imagePicker.setImageLoader(new BMHookGlide());   //设置图片加载器
//        imagePicker.setShowCamera(true);  //
//        imagePicker.setCrop(false);//允许裁剪（单选才有效）
//        imagePicker.setMultiMode(true);//是否是多张
//        imagePicker.setSelectLimit(bean.maxCount);    //选中数量限制
//        Intent intent = new Intent(context, ImageGridActivity.class);
//        PersistentManager persistentManager = ManagerFactory.getManagerService(PersistentManager
//                .class);
//        persistentManager.setCacheData(Constant.ImageConstants.UPLOAD_IMAGE_BEAN, bean);
//        ((Activity) context).startActivityForResult(intent, requestCode);
//
//    }
    // xum add 新增 文件上传
    public void UpMultipleFileData(Context context, ArrayList<MyFileItem> items, UploadFileBean
        bean) {
        ModalManager.BmLoading.showLoading(context, null, false);
        // xum 更改 文件地址 存储容器 update 20180815
        Map<String,String> fileFilrUrl = new HashMap<>();
        if (items != null && items.size() > 0) {
            for (MyFileItem item : items) {
//                // Bitmap bitmap = ImageUtil.getBitmap(item.path, context);
//                String path = new File(FileManager.getTempFilePath(context), String.valueOf
//                        (SystemClock.currentThreadTimeMillis())).getAbsolutePath();
//                String imageFileUrl = ImageUtil.zoomImage(context, bitmap, bean == null ? 0 :
//                        (int) bean.imageWidth, Constant
//                        .ImageConstants.BIGGESTWIDTH, path);
                // imagesFilrUrl.add(imageFileUrl);
                // xum 更改 图片地址 存储容器 update 20180815
                String ext = AppUtils.getFileExtName(item.path);
                fileFilrUrl.put(TextUtils.isEmpty(ext) ? "file.jpg" : item.name,item.path);
                // bitmap.recycle();

            }
        }
        HashMap<String, String> uploadParams = null;
        HashMap<String, String> heads = null;
        if (bean != null) {
            String params = bean.params;
            ParseManager parseManager = ManagerFactory.getManagerService(ParseManager.class);
            uploadParams = parseManager.parseObject(params, HashMap.class);
            heads = parseManager.parseObject(bean.header, HashMap.class);
        }
        AxiosManager axiosManager = ManagerFactory.getManagerService(AxiosManager.class);
        String url = TextUtils.isEmpty(bean.url) ? Api.UPLOAD_URL : bean.url;
        // axiosManager.upload(url, imagesFilrUrl, uploadParams, heads);
        // xum   修改 图片上传处理方法 自己重构了一个 上传方法 update 20180815
        axiosManager.uploadFile(url, fileFilrUrl, uploadParams, heads);
    }





    /**
     * 判断Sd卡是否挂载，是否有Sd卡权限
     */
    private boolean checkPermission(Context context) {
        PermissionManager permissionManager = ManagerFactory.getManagerService(PermissionManager
            .class);
        boolean hasPermisson = permissionManager.hasPermissions(context, Manifest.permission
            .READ_EXTERNAL_STORAGE);
        if (!hasPermisson) {
            ModalManager.BmToast.toast(context, "读取sd卡存储权限未授予，请到应用设置页面开启权限!", Toast.LENGTH_SHORT);
        }
        return hasPermisson;
    }
}

